﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Repositories;
using System.Data;
using System.Text;
using Estsh.Core.Models;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;

namespace Estsh.Core.Wms.Repositories
{
    public class InpdlineRepository : BaseRepository<BaseEntity>, IInpdlineRepository
    {
        public InpdlineRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }
        //领料
        public List<WmsMoveHeader> CheckMoveHeader(string onLine)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select distinct a.*,b.dest_zone_name from wms_move_header as a (nolock) left join wms_move_detail as b (nolock) on a.order_no=b.order_no  
  where a.order_status in (@CREATE,@PROCESSING) and a.order_type in (10,300) and a.enabled='Y' and a.wrong_sign='0' order by a.update_time   ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@order_type",(int)WmsEnumUtil.MoveOrderType.PICK ); 
                parameters.Add("@CREATE", (int)WmsEnumUtil.MoveOrderStatus.CREATE);
                parameters.Add("@PROCESSING", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);

                List<WmsMoveHeader> WmsQcDetail = dbConn.Query<WmsMoveHeader>(getqcSQL.ToString(), parameters).ToList();

                return WmsQcDetail;
            }
        }

        //领料上线
        public List<WmsMoveHeader> CheckOnlineMoveHeader(string onLine, string carton_no)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select distinct a.*,b.dest_zone_name from wms_move_header as a (nolock) left join wms_move_detail as b (nolock) on a.order_no=b.order_no    
                                  left join wms_move_sn as c (nolock) on a.order_no = c.order_no 
                                  where a.order_status in (@CREATE,@PROCESSING,@BATCHOVER) and a.order_type=@order_type and a.wrong_sign='0' and a.enabled='Y' " + (carton_no == null || carton_no == "" ? "" : "and c.carton_no='" + carton_no + "'") + " order by a.update_time   ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_type", (int)WmsEnumUtil.MoveOrderType.PICK);
                parameters.Add("@CREATE", 0);
                parameters.Add("@PROCESSING", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                parameters.Add("@BATCHOVER", (int)WmsEnumUtil.MoveOrderStatus.BATCHOVER);

                List<WmsMoveHeader> WmsQcDetail = dbConn.Query<WmsMoveHeader>(getqcSQL.ToString(), parameters).ToList();

                return WmsQcDetail;
            }
        }

        public List<WmsMoveDetail_Extend> CheckMoveDetail(string orderNo, int status)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select *,(select top 1 locate_name from sys_stock (nolock) where part_id=a.part_id and status=@status  and enabled='Y' and locate_name != '' and locate_name is not null order by create_time)   as recommend ,(select top 1 vendor_name from sys_stock as b left join sys_vendor as c on b.vendor_id = c.vendor_id where b.part_no = a.part_no and vendor_name is not null and vendor_name !='' ) as vendor_name  from wms_move_detail as a (nolock) where order_no=@order_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);
                parameters.Add("@status", status);
                List<WmsMoveDetail_Extend> WmsMoveDetail = dbConn.Query<WmsMoveDetail_Extend>(getqcSQL.ToString(), parameters).ToList();

                return WmsMoveDetail;
            }
        }

        public SysStock GetCartonInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no=@carton_no AND  enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                //List<SysStock> dt = dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
                return dbConn.QueryFirstOrDefault<SysStock>(sql.ToString(), parameters);
            }
        }

        public List<WmsMoveSn> GetMoveSN(string order_no)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_move_sn (nolock) where order_no=@order_no and enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", order_no);

                return dbConn.Query<WmsMoveSn>(sql.ToString(), parameters).ToList();
            }
        }

        public List<SysStock> GetPartByFIFO(string part_id)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append(@"select * from sys_stock (nolock) where part_id=@part_id and status=@status  and enabled='Y' 
                            and create_time=(select min(create_time) from sys_stock (nolock) where part_id=@part_id and status=@status and enabled='Y' )");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@part_id", part_id);
                parameters.Add("@status", (int)WmsEnumUtil.StockStatus.INSTOCKED);
                //List<SysStock> dt = dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }


        public string UpdteOnlineStatus(List<WmsRackPackage_Extend> rackPackageInfo, List<WmsMoveDetail_Extend> detailInfo, List<WmsMoveSn> moveSnlist, string orderNo, int LocateId, string LocateName, string cartonNo, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();


                for (int i = 0; i < rackPackageInfo.Count; i++)
                {
                    //更新条码表，库位为线边
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name  where carton_no=@carton_no ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@locate_id", LocateId); //更新库位
                    updateStringparameters.Add("@locate_name", LocateName);
                    updateStringparameters.Add("@carton_no", rackPackageInfo[i].CortonNo);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);




                    //修改移库明细表  30 处理中，更新配料数量
                    StringBuilder updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"update wms_move_detail set rec_qty=out_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where part_no=@part_no ");
                    DynamicParameters updateStringparameters2 = new DynamicParameters();
                    updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                    updateStringparameters2.Add("@part_no", rackPackageInfo[i].PartNo);
                    //updateStringparameters2.Add("@rec_qty", intSplitQty);
                    updateStringparameters2.Add("@update_userid", loginId);
                    updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString2.ToString());
                    parameterList.Add(updateStringparameters2);

                    //修改移库条码表  40 已完成
                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@"update wms_move_sn set status=@status,update_userid=@update_userid,update_time=@update_time where carton_no=@carton_no ");
                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@status", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                    updateStringparameters4.Add("@carton_no", rackPackageInfo[i].CortonNo);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);


                    //更新周转箱库位
                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name  where carton_no=@carton_no ");
                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@locate_id", LocateId); //更新库位
                    updateStringparameters3.Add("@locate_name", LocateName);
                    updateStringparameters3.Add("@carton_no", cartonNo);
                    updateStringparameters3.Add("@update_userid", loginId);
                    updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);


                }

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public string UpdteInpslineStatus(string orderNo, WmsRackPackage_Extend rackPackageInfo, List<WmsMoveDetail_Extend> detail, SysStock Stock, string loginId, string destRackNo, string SplitQty, string rackOrCartNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                //int intRackNum = Convert.ToInt32(rackPackageInfo.RackNum);
                int intSplitQty = Convert.ToInt32(SplitQty);

                ////修改周转箱中物料条码拆分出的剩余数量
                //StringBuilder updateString8 = new StringBuilder(1024);
                //updateString8.Append(" UPDATE dbo.wms_rack_package SET rack_num=@rack_num  WHERE  ruid=@ruid ");
                //DynamicParameters updateStringparameters8 = new DynamicParameters();
                //updateStringparameters8.Add("@rack_num", intRackNum- intSplitQty);   
                //updateStringparameters8.Add("@ruid", rackPackageInfo.Ruid);
                //updateStringparameters8.Add("@update_userid", loginId);
                //updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                //sqlStrings.Add(updateString8.ToString());
                //parameterList.Add(updateStringparameters8);



                //修改移库主表  30 处理中
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                updateStringparameters.Add("@order_no", detail[0].OrderNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);

                //修改移库明细表  30 处理中，更新配料数量
                StringBuilder updateString2 = new StringBuilder(1024);
                updateString2.Append(@"update wms_move_detail set pick_qty = pick_qty + @pick_qty, out_qty=@out_qty ,rec_qty=@rec_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                updateStringparameters2.Add("@ruid", detail[0].Ruid);
                updateStringparameters2.Add("@out_qty", intSplitQty);
                updateStringparameters2.Add("@pick_qty", intSplitQty);
                updateStringparameters2.Add("@rec_qty", intSplitQty);
                updateStringparameters2.Add("@update_userid", loginId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);


                //修改条码表  60 配料中
                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append("update sys_stock set relevance_order=@relevance_order,status=@status,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@carton_no", Stock.CartonNo);
                updateStringparameters5.Add("@relevance_order", orderNo);
                updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);



                //新增 sys_stock_trans 事务表
                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PRODUCTION_PICK);
                updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                updateStringparameters4.Add("@part_id", Stock.PartId);
                updateStringparameters4.Add("@part_no", Stock.PartNo);
                updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@dest_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@dest_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@old_qty", Stock.Qty);
                updateStringparameters4.Add("@new_qty", Stock.Qty);
                updateStringparameters4.Add("@trans_qty", Stock.Qty);
                updateStringparameters4.Add("@old_status", Stock.Status);
                updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@unit", Stock.Unit);
                updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@enabled", Stock.Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();

                    //updateString2 = new StringBuilder(1024);
                    //updateString2.Append(@"select* from wms_move_detail where order_no = '" + detail[0].OrderNo + "' and qty>pick_qty ");//配料完成 已配料
                    //List<WmsMoveDetail> wmsMoveDetails = dbConn.Query<WmsMoveDetail>(updateString2.ToString()).ToList();
                    //if (wmsMoveDetails.Count == 0)
                    //{
                    //    updateString = new StringBuilder(1024);
                    //    updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");

                    //    updateStringparameters = new DynamicParameters();
                    //    updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.BATCHOVER);
                    //    updateStringparameters.Add("@order_no", detail[0].OrderNo);
                    //    updateStringparameters.Add("@update_userid", loginId);
                    //    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    //    dbConn.Execute(updateString.ToString(), updateStringparameters);
                    //}
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public string UpdteStatus(List<WmsMoveDetail_Extend> detail, SysStock Stock, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                //修改移库主表  30 处理中
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                updateStringparameters.Add("@order_no", detail[0].OrderNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);

                //修改移库明细表  30 处理中
                StringBuilder updateString2 = new StringBuilder(1024);
                updateString2.Append(@"update wms_move_detail set pick_qty = pick_qty + @pick_qty, item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                updateStringparameters2.Add("@ruid", detail[0].Ruid);
                updateStringparameters2.Add("@pick_qty", Stock.Qty);
                updateStringparameters2.Add("@update_userid", loginId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);


                //修改条码表  60 配料中
                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append("update sys_stock set status=@status,update_userid=@update_userid,update_time=@update_time  where carton_no= @carton_no ");
                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@carton_no", Stock.CartonNo);
                updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);


                //插入 wms_move_sn 条码表
                StringBuilder updateString3 = new StringBuilder(1024);
                updateString3.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                        ([order_no]
                                        ,[item_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[carton_no]
                                        ,[qty]
                                        ,[unit]
                                        ,[status]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                         VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                DynamicParameters updateStringparameters3 = new DynamicParameters();
                updateStringparameters3.Add("@order_no", detail[0].OrderNo);
                updateStringparameters3.Add("@item_no", detail[0].ItemNo);
                updateStringparameters3.Add("@part_id", detail[0].PartId);
                updateStringparameters3.Add("@part_no", detail[0].PartNo);
                updateStringparameters3.Add("@part_spec", detail[0].PartSpec);
                updateStringparameters3.Add("@carton_no", Stock.CartonNo);
                updateStringparameters3.Add("@qty", Stock.Qty);
                updateStringparameters3.Add("@unit", Stock.Unit);
                updateStringparameters3.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                updateStringparameters3.Add("@factory_id", detail[0].FactoryId);
                updateStringparameters3.Add("@factory_code", detail[0].FactoryCode);
                updateStringparameters3.Add("@enabled", "Y");
                updateStringparameters3.Add("@update_userid", loginId);
                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@create_userid", loginId);
                updateStringparameters3.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString3.ToString());
                parameterList.Add(updateStringparameters3);


                //新增 sys_stock_trans 事务表
                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PRODUCTION_PICK);
                updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                updateStringparameters4.Add("@part_id", Stock.PartId);
                updateStringparameters4.Add("@part_no", Stock.PartNo);
                updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@dest_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@dest_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@old_qty", Stock.Qty);
                updateStringparameters4.Add("@new_qty", Stock.Qty);
                updateStringparameters4.Add("@trans_qty", Stock.Qty);
                updateStringparameters4.Add("@old_status", Stock.Status);
                updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.BATCHED);
                updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@unit", Stock.Unit);
                updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@enabled", Stock.Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();

                    updateString2 = new StringBuilder(1024);
                    updateString2.Append(@"select* from wms_move_detail where order_no = '" + detail[0].OrderNo + "' and qty>pick_qty ");//配料完成 已配料
                    List<WmsMoveDetail> wmsMoveDetails = dbConn.Query<WmsMoveDetail>(updateString2.ToString()).ToList();
                    if (wmsMoveDetails.Count == 0)
                    {
                        updateString = new StringBuilder(1024);
                        updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");

                        updateStringparameters = new DynamicParameters();
                        updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.BATCHOVER);
                        updateStringparameters.Add("@order_no", detail[0].OrderNo);
                        updateStringparameters.Add("@update_userid", loginId);
                        updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        dbConn.Execute(updateString.ToString(), updateStringparameters);
                    }
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }

        public string BatchingCompleted(string loginId, string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append(@"update wms_move_header set batching_status=1,update_userid=@update_userid,update_time=@update_time where order_no=@order_no");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@order_no", orderNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                int rec = dbConn.Execute(updateString.ToString(), updateStringparameters);
                if (rec > 0)
                {
                    return "OK";
                }
                else
                {
                    return "操作失败，请重新尝试！";
                }
            }
        }

        //上料
        public List<SysLocate> GetLocateInfo(string locate_name)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder LocateInfoSQL = new StringBuilder(1024);
                LocateInfoSQL.Append(@"select B.erp_warehouse,A.* from sys_locate AS A (nolock) LEFT JOIN sys_zone AS B (nolock) ON A.zone_id=B.zone_id   where A.locate_name=@locate_name and A.enabled='Y' and B.enabled='Y'  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locate_name", locate_name);
                List<SysLocate> LocateInfo = dbConn.Query<SysLocate>(LocateInfoSQL.ToString(), parameters).ToList();

                return LocateInfo;
            }
        }

        public List<WmsMoveDetail> CheckMoveDetail2(string orderNo, int status)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"select *,(select top 1 locate_name from sys_locate_part (nolock) where part_id=a.part_id   and enabled='Y' and locate_name != '' and locate_name is not null order by create_time)   as recommend ,(select top 1 vendor_name from sys_stock as b left join sys_vendor as c on b.vendor_id = c.vendor_id where b.part_no = a.part_no and vendor_name is not null and vendor_name !='') as vendor_name    from wms_move_detail as a (nolock) where order_no=@order_no and enabled='Y'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);
                List<WmsMoveDetail> WmsMoveDetail = dbConn.Query<WmsMoveDetail>(getqcSQL.ToString(), parameters).ToList();

                return WmsMoveDetail;
            }
        }

        public List<SysStock> GetLocateCapacity(string locate_id)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder GetLocateSQL = new StringBuilder(1024);
                GetLocateSQL.Append(@" select isnull(sum(qty),0) qty from sys_stock (nolock) where locate_id=@locate_id and status = @status ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locate_id", locate_id);
                parameters.Add("@status", (int)WmsEnumUtil.StockStatus.ONLINED);
                List<SysStock> LocateInfo = dbConn.Query<SysStock>(GetLocateSQL.ToString(), parameters).ToList();

                return LocateInfo;
            }
        }

        public string UpdteStatusOnLine(string loginId, string orderNo, SysStock Stock, List<SysLocate> locate)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                //修改移库主表  30 处理中
                //StringBuilder updateString = new StringBuilder(1024);
                //updateString.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                //DynamicParameters updateStringparameters = new DynamicParameters();
                //updateStringparameters.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                //updateStringparameters.Add("@order_no", detail[0].OrderNo);
                //updateStringparameters.Add("@update_userid", loginId);
                //updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                //sqlStrings.Add(updateString.ToString());
                //parameterList.Add(updateStringparameters);


                //修改移库明细表  已收数,
                StringBuilder updateString2 = new StringBuilder(1024);
                updateString2.Append(@"update wms_move_detail set out_qty = out_qty + @out_qty,update_userid=@update_userid,update_time=@update_time,item_status = case when pick_qty = out_qty + @out_qty then @item_status else item_status end  where order_no=@order_no and part_id=@part_id ");
                DynamicParameters updateStringparameters2 = new DynamicParameters();
                updateStringparameters2.Add("@order_no", orderNo);
                updateStringparameters2.Add("@part_id", Stock.PartId);
                updateStringparameters2.Add("@out_qty", Stock.Qty);
                updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED);
                updateStringparameters2.Add("@update_userid", loginId);
                updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString2.ToString());
                parameterList.Add(updateStringparameters2);

                //修改移库条码表  40 已上线
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append(" update wms_move_sn set status=@status,update_userid=@update_userid,update_time=@update_time where carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.ONLINED);
                updateStringparameters.Add("@carton_no", Stock.CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);

                //修改条码表  70 已上线
                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append("update sys_stock set status=@status,zone_id=@zone_id,zone_name=@zone_name,erp_warehouse=@erp_warehouse,update_userid=@update_userid,update_time=@update_time,locate_id=@locate_id,locate_name=@locate_name  where carton_no= @carton_no ");
                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@carton_no", Stock.CartonNo);
                updateStringparameters5.Add("@status", (int)WmsEnumUtil.StockStatus.ONLINED);
                updateStringparameters5.Add("@locate_id", locate[0].LocateId);
                updateStringparameters5.Add("@locate_name", locate[0].LocateName);
                updateStringparameters5.Add("@zone_id", locate[0].ZoneId);
                updateStringparameters5.Add("@zone_name", locate[0].ZoneName);
                updateStringparameters5.Add("@erp_warehouse", locate[0].ErpWarehouse);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);


                //新增 sys_stock_trans 事务表
                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PICK_ONLINE);
                updateStringparameters4.Add("@carton_no", Stock.CartonNo);
                updateStringparameters4.Add("@part_id", Stock.PartId);
                updateStringparameters4.Add("@part_no", Stock.PartNo);
                updateStringparameters4.Add("@part_spec", Stock.PartSpec);
                updateStringparameters4.Add("@src_locate_id", Stock.LocateId);
                updateStringparameters4.Add("@src_locate_name", Stock.LocateName);
                updateStringparameters4.Add("@dest_locate_id", locate[0].LocateId);
                updateStringparameters4.Add("@dest_locate_name", locate[0].LocateName);
                updateStringparameters4.Add("@old_qty", Stock.Qty);
                updateStringparameters4.Add("@new_qty", Stock.Qty);
                updateStringparameters4.Add("@trans_qty", Stock.Qty);
                updateStringparameters4.Add("@old_status", Stock.Status);
                updateStringparameters4.Add("@new_status", (int)WmsEnumUtil.StockStatus.ONLINED);
                updateStringparameters4.Add("@old_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@new_qms_status", Stock.QmsStatus);
                updateStringparameters4.Add("@unit", Stock.Unit);
                updateStringparameters4.Add("@factory_id", Stock.FactoryId);
                updateStringparameters4.Add("@factory_code", Stock.FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", Stock.ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", Stock.WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", Stock.WarehouseName);
                updateStringparameters4.Add("@src_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@src_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@dest_zone_id", Stock.ZoneId);
                updateStringparameters4.Add("@dest_zone_name", Stock.ZoneName);
                updateStringparameters4.Add("@enabled", Stock.Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);

                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();

                    StringBuilder getqcSQL = new StringBuilder(1024);
                    getqcSQL.Append(@"select a.ref_order_no,b.* from wms_move_header as a (nolock) left join wms_move_detail as b (nolock) on a.order_no=b.order_no  where a.order_no=@order_no and b.enabled='Y'");
                    DynamicParameters parameters = new DynamicParameters();
                    parameters.Add("@order_no", orderNo);
                    List<WmsMoveDetail> WmsMoveDetail = dbConn.Query<WmsMoveDetail>(getqcSQL.ToString(), parameters).ToList();

                    if (WmsMoveDetail.Sum(a => a.ItemStatus) == (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED * WmsMoveDetail.Count)
                    {
                        List<string> sqlStrings2 = new List<string>();
                        List<DynamicParameters> parameterList2 = new List<DynamicParameters>();
                        //修改移库条码表  40 已上线
                        StringBuilder updateString6 = new StringBuilder(1024);
                        updateString6.Append(" update wms_move_header set order_status =@order_status ,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ; ");
                        DynamicParameters updateStringparameters6 = new DynamicParameters();
                        updateStringparameters6.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.COMPLETED);
                        updateStringparameters6.Add("@order_no", orderNo);
                        updateStringparameters6.Add("@update_userid", loginId);
                        updateStringparameters6.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                        sqlStrings2.Add(updateString6.ToString());
                        parameterList2.Add(updateStringparameters6);
                        //dbConn.Execute(updateString6.ToString(), updateStringparameters6);

                        if (WmsMoveDetail[0].RefOrderNo == "安全库存拉动")
                        {
                            foreach (var MoveDetail in WmsMoveDetail)
                            {
                                StringBuilder updateString7 = new StringBuilder(1024);
                                updateString7.Append("update  sys_locate_part set is_safety_pull='0' where part_id=@part_id ");
                                DynamicParameters updateStringparameters7 = new DynamicParameters();
                                updateStringparameters7.Add("@part_id", MoveDetail.PartId);

                                sqlStrings2.Add(updateString7.ToString());
                                parameterList2.Add(updateStringparameters7);
                            }
                        }
                        IDbTransaction transaction2 = dbConn.BeginTransaction();
                        bool successCount2 = true;
                        for (int i = 0; i < sqlStrings2.Count; i++)
                        {
                            if (dbConn.Execute(sqlStrings2[i], parameterList2[i], transaction2) < 0)
                            {
                                successCount2 = false;
                            }
                        }
                        if (successCount2)
                        {
                            transaction2.Commit();
                        }
                        else
                        {
                            transaction.Rollback();
                        }
                    }

                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        //大件拣选
        public List<WmsPullingOrder> GetPullingOrderList(string Line, string MaterialType)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"SELECT * FROM [wms_pulling_order] (nolock) where pulling_type like '%" + MaterialType + "%' and pdline_id like '%" + Line + "%'");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@pulling_type", MaterialType);
                parameters.Add("@pdline_id", Line);

                List<WmsPullingOrder> WmsPullingOrder = dbConn.Query<WmsPullingOrder>(getqcSQL.ToString()).ToList();

                return WmsPullingOrder;
            }
        }

        public List<WmsPullingOrderDetail> GetPullingOrderDetails(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"SELECT * FROM [wms_pulling_order_detail] (nolock) where order_no=@order_no order by work_order_seq ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);

                List<WmsPullingOrderDetail> WmsPullingOrder = dbConn.Query<WmsPullingOrderDetail>(getqcSQL.ToString(), parameters).ToList();

                return WmsPullingOrder;
            }
        }

        public List<WmsRack> GetRkInfos(string RKNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder getqcSQL = new StringBuilder(1024);
                getqcSQL.Append(@"SELECT * FROM [wms_rack] (nolock) where rack_no=@rack_no  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", RKNo);

                List<WmsRack> WmsRack = dbConn.Query<WmsRack>(getqcSQL.ToString(), parameters).ToList();

                return WmsRack;
            }
        }

        public List<SysStock> GetRackPackage(string RKNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                StringBuilder RackPackageSQL = new StringBuilder(1024);
                RackPackageSQL.Append(@"SELECT b.* FROM wms_rack_package as a (nolock) left join sys_stock as b (nolock) on a.corton_no=b.carton_no where a.rack_no=@rack_no and a.enabled = 'Y' order by qty  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", RKNo);

                List<SysStock> WmsRackPackage = dbConn.Query<SysStock>(RackPackageSQL.ToString(), parameters).ToList();

                return WmsRackPackage;
            }
        }

        public List<SysStock> GetCartonInfoByRK(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no=@carton_no and enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                //List<SysStock> dt = dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }

        public string UpdatePullingOrderDetails(List<WmsPullingOrderDetail> detail, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < detail.Count; i++)
                {
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append(" update [wms_pulling_order_detail] set item_status='20' where guid=@guid ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@guid", detail[i].Guid);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);
                }

                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public List<SysStock> GetMaterialInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from sys_stock (nolock) where carton_no= @carton_no ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.Query<SysStock>(sql.ToString(), parameters).ToList();
            }
        }
        public List<SysStock> CheckMaterialSplit(List<SysStock> oldStock, int splitNum, string loginId, int splitCount)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                decimal oldQty = oldStock[0].Qty;
                decimal newQty = oldStock[0].Qty - splitNum;

                //更新条码表，数量
                StringBuilder updateString = new StringBuilder(1024);
                updateString.Append("update sys_stock set qty=@qty  where carton_no=@carton_no ");
                DynamicParameters updateStringparameters = new DynamicParameters();
                updateStringparameters.Add("@qty", newQty);
                updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters.Add("@update_userid", loginId);
                updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                sqlStrings.Add(updateString.ToString());
                parameterList.Add(updateStringparameters);


                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString4 = new StringBuilder(1024);
                updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters4 = new DynamicParameters();
                updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters4.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters4.Add("@old_qty", oldQty);
                updateStringparameters4.Add("@new_qty", newQty);
                updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters4.Add("@old_status", oldStock[0].Status);
                updateStringparameters4.Add("@new_status", oldStock[0].Status);
                updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters4.Add("@unit", oldStock[0].Unit);
                updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters4.Add("@update_userid", loginId);
                updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@create_userid", loginId);
                updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters4.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString4.ToString());
                parameterList.Add(updateStringparameters4);
                #endregion

                oldStock[0].CartonNo = checkCartonNo(oldStock[0].CartonNo, splitCount);

                oldStock[0].Qty = splitNum;
                //插入拆分出的箱条码
                #region 插入拆分出的箱条码

                StringBuilder updateString3 = new StringBuilder(1024);
                updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                DynamicParameters updateStringparameters3 = new DynamicParameters();
                updateStringparameters3.Add("@vendor_id", oldStock[0].VendorId);
                updateStringparameters3.Add("@vendor_code", oldStock[0].VendorName);
                updateStringparameters3.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                updateStringparameters3.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters3.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters3.Add("@lot_no", oldStock[0].LotNo);
                updateStringparameters3.Add("@fix_lot_no", oldStock[0].FixLotNo);
                updateStringparameters3.Add("@status", oldStock[0].Status);
                updateStringparameters3.Add("@qty", splitNum);
                updateStringparameters3.Add("@snp_qty", oldStock[0].SnpQty);
                updateStringparameters3.Add("@locate_id", oldStock[0].LocateId);
                updateStringparameters3.Add("@locate_name", oldStock[0].LocateName);
                updateStringparameters3.Add("@group_no", oldStock[0].GroupNo);
                updateStringparameters3.Add("@erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters3.Add("@date_code", oldStock[0].DateCode);
                updateStringparameters3.Add("@qms_status", oldStock[0].QmsStatus);
                updateStringparameters3.Add("@ref_order_no", oldStock[0].RefOrderNo);
                updateStringparameters3.Add("@unit", oldStock[0].Unit);
                updateStringparameters3.Add("@dock", oldStock[0].Dock);
                updateStringparameters3.Add("@warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters3.Add("@warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters3.Add("@zone_id", oldStock[0].ZoneId);
                updateStringparameters3.Add("@zone_name", oldStock[0].ZoneName);
                updateStringparameters3.Add("@printed", oldStock[0].Printed);
                updateStringparameters3.Add("@print_time", oldStock[0].PrintTime);
                updateStringparameters3.Add("@remark", oldStock[0].Remark);
                updateStringparameters3.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters3.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters3.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters3.Add("@update_userid", loginId);
                updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters3.Add("@create_userid", oldStock[0].CreateUserId);
                updateStringparameters3.Add("@create_time", oldStock[0].CreateTime);
                updateStringparameters3.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString3.ToString());
                parameterList.Add(updateStringparameters3);
                #endregion

                //新增 sys_stock_trans 事务表
                #region sys_stock_trans 事务表

                StringBuilder updateString5 = new StringBuilder(1024);
                updateString5.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                DynamicParameters updateStringparameters5 = new DynamicParameters();
                updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                updateStringparameters5.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters5.Add("@part_id", oldStock[0].PartId);
                updateStringparameters5.Add("@part_no", oldStock[0].PartNo);
                updateStringparameters5.Add("@part_spec", oldStock[0].PartSpec);
                updateStringparameters5.Add("@src_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@src_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@dest_locate_id", oldStock[0].LocateId);
                updateStringparameters5.Add("@dest_locate_name", oldStock[0].LocateName);
                updateStringparameters5.Add("@old_qty", 0);
                updateStringparameters5.Add("@new_qty", splitNum);
                updateStringparameters5.Add("@trans_qty", oldStock[0].Qty);
                updateStringparameters5.Add("@old_status", oldStock[0].Status);
                updateStringparameters5.Add("@new_status", oldStock[0].Status);
                updateStringparameters5.Add("@old_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@new_qms_status", oldStock[0].QmsStatus);
                updateStringparameters5.Add("@unit", oldStock[0].Unit);
                updateStringparameters5.Add("@factory_id", oldStock[0].FactoryId);
                updateStringparameters5.Add("@factory_code", oldStock[0].FactoryCode);
                updateStringparameters5.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                updateStringparameters5.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                updateStringparameters5.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                updateStringparameters5.Add("@src_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@src_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@dest_zone_id", oldStock[0].ZoneId);
                updateStringparameters5.Add("@dest_zone_name", oldStock[0].ZoneName);
                updateStringparameters5.Add("@enabled", oldStock[0].Enabled);
                updateStringparameters5.Add("@update_userid", loginId);
                updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@create_userid", loginId);
                updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters5.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString5.ToString());
                parameterList.Add(updateStringparameters5);
                #endregion


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }
        public string checkCartonNo(string carton, int splitCount)
        {
            List<SysStock> MaterialInfo = GetMaterialInfo(carton + "-" + splitCount);
            if (MaterialInfo.Count > 0)
            {
                //有重复
                return checkCartonNo(carton, splitCount + 1);
            }
            else
            {
                return carton + "-" + splitCount;
            }
        }

        public string UpdateLargePick(List<string> sqlStrings, List<DynamicParameters> parameterList)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();



                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return "操作失败，请重新尝试！";
                }
            }
        }

        public List<SysPdline> GetPdline(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select b.* from wms_pulling_order as a (nolock) left join sys_pdline as b (nolock) on a.pdline_id=b.pdline_id where a.order_no= @order_no ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);

                return dbConn.Query<SysPdline>(sql.ToString(), parameters).ToList();
            }
        }

        public string SetMoveOrderClose(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                List<string> sqlLists = new List<string>();
                List<DynamicParameters> parameters = new List<DynamicParameters>();
                DynamicParameters dynamic = new DynamicParameters();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);

                SqlStringBuilder.Append("update wms_move_header set order_status='60',update_userid=@updateUserid,update_time = CONVERT(varchar(50), GETDATE(), 21)  WHERE order_no=@order_no");
                sqlLists.Add(SqlStringBuilder.ToString());
                dynamic = new DynamicParameters();
                dynamic.Add("@updateUserid", 0);
                dynamic.Add("@order_no", orderNo);
                parameters.Add(dynamic);


                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("update wms_move_detail set item_status='100',update_userid=@updateUserid,update_time = CONVERT(varchar(50), GETDATE(), 21)  WHERE order_no=@order_no");
                sqlLists.Add(SqlStringBuilder.ToString());
                dynamic = new DynamicParameters();
                dynamic.Add("@updateUserid", 0);
                dynamic.Add("@order_no", orderNo);
                parameters.Add(dynamic);

                string sql = "SELECT * FROM wms_move_header (NOLOCK) WHERE order_no ='" + orderNo + "'";
                WmsMoveHeader wmsMoveHeaders = dbConn.Query<WmsMoveHeader>(sql).FirstOrDefault();
                if (wmsMoveHeaders != null)
                {
                    if (wmsMoveHeaders.OrderType == 10 && wmsMoveHeaders.RefOrderNo == "安全库存拉动")//生产领料单
                    {
                        string sqldetail = "SELECT * FROM wms_move_detail (NOLOCK) WHERE order_no ='" + wmsMoveHeaders.OrderNo + "'";
                        List<WmsMoveDetail> wmsMoveDetails = dbConn.Query<WmsMoveDetail>(sqldetail).ToList();
                        for (int j = 0; j < wmsMoveDetails.Count; j++)
                        {
                            SqlStringBuilder = new StringBuilder(1024);//关闭，重置默认库存拉动0
                            SqlStringBuilder.Append("update sys_locate_part set is_safety_pull=0,update_userid=@updateUserid,update_time=@updateTime where part_no=@part_no");
                            sqlLists.Add(SqlStringBuilder.ToString());
                            dynamic = new DynamicParameters();
                            dynamic.Add("@updateUserid", 0);
                            dynamic.Add("@updateTime", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            dynamic.Add("@part_no", wmsMoveDetails[j].PartNo);

                            parameters.Add(dynamic);
                        }
                    }
                }

                bool flag = ExecuteTransaction(sqlLists, parameters);
                if (flag)
                {
                    return "OK";
                }
                else
                {
                    return "操作失败，请重新尝试！";
                }
            }
        }


        public SysStock GetStockCartonInfo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  SELECT * FROM dbo.sys_stock(NOLOCK) WHERE  enabled='Y' AND carton_no IN (  SELECT rack_no FROM dbo.wms_rack WHERE  enabled='Y' AND packageType=1 AND rack_no=@carton_no) ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysStock>(sql.ToString(), parameters);
            }
        }


        public WmsRackPackage_Extend GetRackPackageInfo2(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  select * from wms_rack_package (nolock) where corton_no= @corton_no and enabled='Y'  ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@corton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<WmsRackPackage_Extend>(sql.ToString(), parameters);
            }
        }


        public List<WmsRackPackage_Extend> GetRackPackageInfo(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("select * from wms_rack_package (nolock) where rack_no= @rack_no and enabled='Y' ORDER BY create_time DESC ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@rack_no", rackNo);

                return dbConn.Query<WmsRackPackage_Extend>(sql.ToString(), parameters).ToList();
            }
        }


        public List<WmsMoveSn> GetMoveSnList(string orderNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("SELECT * FROM dbo.wms_move_sn WHERE order_no=@order_no AND enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderNo);

                return dbConn.Query<WmsMoveSn>(sql.ToString(), parameters).ToList();
            }
        }



        /// <summary>
        /// 生成领料(一步)-拆分
        /// </summary>
        /// <param name="oldStock"></param>
        /// <param name="splitNum"></param>
        /// <param name="loginId"></param>
        /// <param name="splitCount"></param>
        /// <returns></returns>
        public List<SysStock> CheckPickMaterialSplit(WmsRackPackage_Extend rackPackageInfo, string destRackNo, string locateId, string locateCode, string rackNo, List<WmsMoveDetail_Extend> detInfo, List<SysStock> oldStock, int splitNum, string loginId, int splitCount)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                decimal oldQty = oldStock[0].Qty;
                decimal newQty = oldStock[0].Qty - splitNum;

                if (newQty != 0)
                {
                    //更新条码表，数量、库位
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append("update sys_stock set qty=@qty,locate_id=@locate_id,locate_name=@locate_name  where carton_no=@carton_no ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@qty", newQty);
                    updateStringparameters.Add("@locate_id", locateId); //更新库位
                    updateStringparameters.Add("@locate_name", locateCode);
                    updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);


                    //新增 sys_stock_trans 事务表
                    #region sys_stock_trans 事务表

                    StringBuilder updateString4 = new StringBuilder(1024);
                    updateString4.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters4 = new DynamicParameters();
                    updateStringparameters4.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                    updateStringparameters4.Add("@carton_no", oldStock[0].CartonNo);
                    updateStringparameters4.Add("@part_id", oldStock[0].PartId);
                    updateStringparameters4.Add("@part_no", oldStock[0].PartNo);
                    updateStringparameters4.Add("@part_spec", oldStock[0].PartSpec);
                    updateStringparameters4.Add("@src_locate_id", oldStock[0].LocateId);
                    updateStringparameters4.Add("@src_locate_name", oldStock[0].LocateName);
                    updateStringparameters4.Add("@dest_locate_id", oldStock[0].LocateId);
                    updateStringparameters4.Add("@dest_locate_name", oldStock[0].LocateName);
                    updateStringparameters4.Add("@old_qty", oldQty);
                    updateStringparameters4.Add("@new_qty", newQty);
                    updateStringparameters4.Add("@trans_qty", oldStock[0].Qty);
                    updateStringparameters4.Add("@old_status", oldStock[0].Status);
                    updateStringparameters4.Add("@new_status", oldStock[0].Status);
                    updateStringparameters4.Add("@old_qms_status", oldStock[0].QmsStatus);
                    updateStringparameters4.Add("@new_qms_status", oldStock[0].QmsStatus);
                    updateStringparameters4.Add("@unit", oldStock[0].Unit);
                    updateStringparameters4.Add("@factory_id", oldStock[0].FactoryId);
                    updateStringparameters4.Add("@factory_code", oldStock[0].FactoryCode);
                    updateStringparameters4.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                    updateStringparameters4.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                    updateStringparameters4.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                    updateStringparameters4.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                    updateStringparameters4.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                    updateStringparameters4.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                    updateStringparameters4.Add("@src_zone_id", oldStock[0].ZoneId);
                    updateStringparameters4.Add("@src_zone_name", oldStock[0].ZoneName);
                    updateStringparameters4.Add("@dest_zone_id", oldStock[0].ZoneId);
                    updateStringparameters4.Add("@dest_zone_name", oldStock[0].ZoneName);
                    updateStringparameters4.Add("@enabled", oldStock[0].Enabled);
                    updateStringparameters4.Add("@update_userid", loginId);
                    updateStringparameters4.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@create_userid", loginId);
                    updateStringparameters4.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters4.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString4.ToString());
                    parameterList.Add(updateStringparameters4);
                    #endregion

                    oldStock[0].CartonNo = checkCartonNo(oldStock[0].CartonNo, splitCount);

                    oldStock[0].Qty = splitNum;
                }
                else
                {
                    //更新条码表，库位
                    StringBuilder updateString = new StringBuilder(1024);
                    updateString.Append("update sys_stock set locate_id=@locate_id,locate_name=@locate_name  where carton_no=@carton_no ");
                    DynamicParameters updateStringparameters = new DynamicParameters();
                    updateStringparameters.Add("@locate_id", locateId); //更新库位
                    updateStringparameters.Add("@locate_name", locateCode);
                    updateStringparameters.Add("@carton_no", oldStock[0].CartonNo);
                    updateStringparameters.Add("@update_userid", loginId);
                    updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                    sqlStrings.Add(updateString.ToString());
                    parameterList.Add(updateStringparameters);
                }


                //插入 wms_move_sn 条码表
                StringBuilder updateString8 = new StringBuilder(1024);
                updateString8.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                        ([order_no]
                                        ,[item_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[carton_no]
                                        ,[qty]
                                        ,[unit]
                                        ,[status]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                         VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                DynamicParameters updateStringparameters8 = new DynamicParameters();
                updateStringparameters8.Add("@order_no", detInfo[0].OrderNo);
                updateStringparameters8.Add("@item_no", detInfo[0].ItemNo);
                updateStringparameters8.Add("@part_id", detInfo[0].PartId);
                updateStringparameters8.Add("@part_no", detInfo[0].PartNo);
                updateStringparameters8.Add("@part_spec", detInfo[0].PartSpec);
                updateStringparameters8.Add("@carton_no", oldStock[0].CartonNo);
                updateStringparameters8.Add("@qty", splitNum);
                updateStringparameters8.Add("@unit", oldStock[0].Unit);
                updateStringparameters8.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                updateStringparameters8.Add("@factory_id", detInfo[0].FactoryId);
                updateStringparameters8.Add("@factory_code", detInfo[0].FactoryCode);
                updateStringparameters8.Add("@enabled", "Y");
                updateStringparameters8.Add("@update_userid", loginId);
                updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters8.Add("@create_userid", loginId);
                updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters8.Add("@guid", Guid.NewGuid());

                sqlStrings.Add(updateString8.ToString());
                parameterList.Add(updateStringparameters8);



                //箱条码存在绑定关系，拆分数量与原数量一致更新周转箱为目的周转箱；拆分数量与原数量不一致，更新原周转箱数量，插入拆分后的新条码周转箱是目的周转箱；不存在直接插入
                if (newQty == 0)
                {
                    //拆分数量一致，更新周转箱为目的周转箱
                    if (rackPackageInfo != null)
                    {
                        StringBuilder updateString2 = new StringBuilder(1024);
                        updateString2.Append("UPDATE dbo.wms_rack_package SET rack_no = @rack_no ,update_userid = @update_userid ,update_time = @update_time WHERE ruid = @ruid ");
                        DynamicParameters updateStringparameters9 = new DynamicParameters();
                        updateStringparameters9.Add("@rack_no", destRackNo);
                        updateStringparameters9.Add("@ruid", rackPackageInfo.Ruid);
                        updateStringparameters9.Add("@update_userid", loginId);
                        updateStringparameters9.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        sqlStrings.Add(updateString2.ToString());
                        parameterList.Add(updateStringparameters9);
                    }

                }
                else
                {
                    if (rackPackageInfo != null)
                    {
                        int intRackNmu = Convert.ToInt32(rackPackageInfo.RackNum);
                        int rackNum = intRackNmu - splitNum;
                        //更新周转箱物料条码中的数量
                        StringBuilder updateString2 = new StringBuilder(1024);
                        updateString2.Append("UPDATE dbo.wms_rack_package SET rack_num = @rack_num ,update_userid = @update_userid ,update_time = @update_time WHERE ruid = @ruid ");
                        DynamicParameters updateStringparameters9 = new DynamicParameters();
                        updateStringparameters9.Add("@rack_num", rackNum);
                        updateStringparameters9.Add("@ruid", rackPackageInfo.Ruid);
                        updateStringparameters9.Add("@update_userid", loginId);
                        updateStringparameters9.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                        sqlStrings.Add(updateString2.ToString());
                        parameterList.Add(updateStringparameters9);
                    }

                    //插入拆分出的箱条码
                    #region 插入拆分出的箱条码

                    StringBuilder updateString3 = new StringBuilder(1024);
                    updateString3.Append(@" INSERT INTO [dbo].[sys_stock]
                                        ([vendor_id]
                                        ,[vendor_code]
                                        ,[carton_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[lot_no]
                                        ,[fix_lot_no]
                                        ,[status]
                                        ,[qty]
                                        ,[snp_qty]
                                        ,[locate_id]
                                        ,[locate_name]
                                        ,[group_no]
                                        ,[erp_warehouse]
                                        ,[date_code]
                                        ,[qms_status]
                                        ,[ref_order_no]
                                        ,[unit]
                                        ,[dock]
                                        ,[warehouse_id]
                                        ,[warehouse_name]
                                        ,[zone_id]
                                        ,[zone_name]
                                        ,[printed]
                                        ,[print_time]
                                        ,[remark]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                        VALUES(@vendor_id,@vendor_code,@carton_no,@part_id,@part_no,@part_spec,@lot_no,@fix_lot_no,@status,@qty,@snp_qty
,@locate_id,@locate_name,@group_no,@erp_warehouse,@date_code,@qms_status,@ref_order_no,@unit,@dock,@warehouse_id,@warehouse_name,@zone_id,@zone_name
,@printed,@print_time,@remark,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid )");

                    DynamicParameters updateStringparameters3 = new DynamicParameters();
                    updateStringparameters3.Add("@vendor_id", oldStock[0].VendorId);
                    updateStringparameters3.Add("@vendor_code", oldStock[0].VendorName);
                    updateStringparameters3.Add("@carton_no", oldStock[0].CartonNo);
                    updateStringparameters3.Add("@part_id", oldStock[0].PartId);
                    updateStringparameters3.Add("@part_no", oldStock[0].PartNo);
                    updateStringparameters3.Add("@part_spec", oldStock[0].PartSpec);
                    updateStringparameters3.Add("@lot_no", oldStock[0].LotNo);
                    updateStringparameters3.Add("@fix_lot_no", oldStock[0].FixLotNo);
                    updateStringparameters3.Add("@status", 60); //原状态
                    updateStringparameters3.Add("@qty", splitNum);
                    updateStringparameters3.Add("@snp_qty", oldStock[0].SnpQty);
                    updateStringparameters3.Add("@locate_id", locateId);
                    updateStringparameters3.Add("@locate_name", locateCode);  //领料单据中的目标库位
                    updateStringparameters3.Add("@group_no", oldStock[0].GroupNo);
                    updateStringparameters3.Add("@erp_warehouse", oldStock[0].ErpWarehouse);
                    updateStringparameters3.Add("@date_code", oldStock[0].DateCode);
                    updateStringparameters3.Add("@qms_status", oldStock[0].QmsStatus);
                    updateStringparameters3.Add("@ref_order_no", oldStock[0].RefOrderNo);
                    updateStringparameters3.Add("@unit", oldStock[0].Unit);
                    updateStringparameters3.Add("@dock", oldStock[0].Dock);
                    updateStringparameters3.Add("@warehouse_id", oldStock[0].WarehouseId);
                    updateStringparameters3.Add("@warehouse_name", oldStock[0].WarehouseName);
                    updateStringparameters3.Add("@zone_id", oldStock[0].ZoneId);
                    updateStringparameters3.Add("@zone_name", oldStock[0].ZoneName);
                    updateStringparameters3.Add("@printed", oldStock[0].Printed);
                    updateStringparameters3.Add("@print_time", oldStock[0].PrintTime);
                    updateStringparameters3.Add("@remark", oldStock[0].Remark);
                    updateStringparameters3.Add("@factory_id", oldStock[0].FactoryId);
                    updateStringparameters3.Add("@factory_code", oldStock[0].FactoryCode);
                    updateStringparameters3.Add("@enabled", oldStock[0].Enabled);
                    updateStringparameters3.Add("@update_userid", loginId);
                    updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters3.Add("@create_userid", oldStock[0].CreateUserId);
                    updateStringparameters3.Add("@create_time", oldStock[0].CreateTime);
                    updateStringparameters3.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString3.ToString());
                    parameterList.Add(updateStringparameters3);
                    #endregion


                    //新增 sys_stock_trans 事务表
                    #region sys_stock_trans 事务表

                    StringBuilder updateString5 = new StringBuilder(1024);
                    updateString5.Append(@" INSERT INTO[dbo].[sys_stock_trans]
                                                    ([trans_code]
                                                    ,[carton_no]
                                                    ,[part_id]
                                                    ,[part_no]
                                                    ,[part_spec]
                                                    ,[src_locate_id]
                                                    ,[src_locate_name]
                                                    ,[dest_locate_id]
                                                    ,[dest_locate_name]
                                                    ,[old_qty]
                                                    ,[new_qty]
                                                    ,[trans_qty]
                                                    ,[old_status]
                                                    ,[new_status]
                                                    ,[old_qms_status]
                                                    ,[new_qms_status]
                                                    ,[unit]
                                                    ,[factory_id]
                                                    ,[factory_code]
                                                    ,[src_erp_warehouse]
                                                    ,[dest_erp_warehouse]
                                                    ,[src_warehouse_id]
                                                    ,[src_warehouse_name]
                                                    ,[dest_warehouse_id]
                                                    ,[dest_warehouse_name]
                                                    ,[src_zone_id]
                                                    ,[src_zone_name]
                                                    ,[dest_zone_id]
                                                    ,[dest_zone_name]
                                                    ,[enabled]
                                                    ,[update_userid]
                                                    ,[update_time]
                                                    ,[create_userid]
                                                    ,[create_time]
                                                    ,[guid])
                                                  VALUES (@trans_code,@carton_no,@part_id,@part_no,@part_spec,@src_locate_id ,@src_locate_name ,@dest_locate_id ,@dest_locate_name ,@old_qty 
                                        ,@new_qty ,@trans_qty ,@old_status ,@new_status ,@old_qms_status ,@new_qms_status ,@unit ,@factory_id ,@factory_code ,@src_erp_warehouse ,@dest_erp_warehouse ,@src_warehouse_id ,@src_warehouse_name ,@dest_warehouse_id ,@dest_warehouse_name ,@src_zone_id ,@src_zone_name ,@dest_zone_id ,@dest_zone_name ,@enabled ,@update_userid ,@update_time ,@create_userid ,@create_time ,@guid )");

                    DynamicParameters updateStringparameters5 = new DynamicParameters();
                    updateStringparameters5.Add("@trans_code", (int)WmsEnumUtil.TransType.PART_SPLIT);
                    updateStringparameters5.Add("@carton_no", oldStock[0].CartonNo);
                    updateStringparameters5.Add("@part_id", oldStock[0].PartId);
                    updateStringparameters5.Add("@part_no", oldStock[0].PartNo);
                    updateStringparameters5.Add("@part_spec", oldStock[0].PartSpec);
                    updateStringparameters5.Add("@src_locate_id", oldStock[0].LocateId);
                    updateStringparameters5.Add("@src_locate_name", oldStock[0].LocateName);
                    updateStringparameters5.Add("@dest_locate_id", oldStock[0].LocateId);
                    updateStringparameters5.Add("@dest_locate_name", oldStock[0].LocateName);
                    updateStringparameters5.Add("@old_qty", 0);
                    updateStringparameters5.Add("@new_qty", splitNum);
                    updateStringparameters5.Add("@trans_qty", oldStock[0].Qty);
                    updateStringparameters5.Add("@old_status", oldStock[0].Status);
                    updateStringparameters5.Add("@new_status", oldStock[0].Status);
                    updateStringparameters5.Add("@old_qms_status", oldStock[0].QmsStatus);
                    updateStringparameters5.Add("@new_qms_status", oldStock[0].QmsStatus);
                    updateStringparameters5.Add("@unit", oldStock[0].Unit);
                    updateStringparameters5.Add("@factory_id", oldStock[0].FactoryId);
                    updateStringparameters5.Add("@factory_code", oldStock[0].FactoryCode);
                    updateStringparameters5.Add("@src_erp_warehouse", oldStock[0].ErpWarehouse);
                    updateStringparameters5.Add("@dest_erp_warehouse", oldStock[0].ErpWarehouse);
                    updateStringparameters5.Add("@src_warehouse_id", oldStock[0].WarehouseId);
                    updateStringparameters5.Add("@src_warehouse_name", oldStock[0].WarehouseName);
                    updateStringparameters5.Add("@dest_warehouse_id", oldStock[0].WarehouseId);
                    updateStringparameters5.Add("@dest_warehouse_name", oldStock[0].WarehouseName);
                    updateStringparameters5.Add("@src_zone_id", oldStock[0].ZoneId);
                    updateStringparameters5.Add("@src_zone_name", oldStock[0].ZoneName);
                    updateStringparameters5.Add("@dest_zone_id", oldStock[0].ZoneId);
                    updateStringparameters5.Add("@dest_zone_name", oldStock[0].ZoneName);
                    updateStringparameters5.Add("@enabled", oldStock[0].Enabled);
                    updateStringparameters5.Add("@update_userid", loginId);
                    updateStringparameters5.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters5.Add("@create_userid", loginId);
                    updateStringparameters5.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    updateStringparameters5.Add("@guid", Guid.NewGuid());

                    sqlStrings.Add(updateString5.ToString());
                    parameterList.Add(updateStringparameters5);
                    #endregion

                }
                //插入 wms_rack_package 周转箱明细表
                StringBuilder insterString2 = new StringBuilder(1024);
                DynamicParameters updateStringparameters7 = new DynamicParameters();
                insterString2.Append(@"INSERT INTO [dbo].[wms_rack_package]
                                 ([rack_no]
                                 ,[corton_no]
                                 ,[packageType]
                                 ,[part_no]
                                 ,[factory_id]
                                 ,[factory_code]
                                 ,[enabled]
                                 ,[create_userid]
                                 ,[create_time]
                                 ,[update_userid]
                                 ,[update_time]
                                 ,[guid],[rack_num])
                           VALUES (@rack_no,@corton_no,@pack_type,@part_no,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid,@rack_num)");
                updateStringparameters7 = new DynamicParameters();
                updateStringparameters7.Add("@rack_no", destRackNo); //目的周转箱
                updateStringparameters7.Add("@corton_no", oldStock[0].CartonNo);
                updateStringparameters7.Add("@pack_type", 0);
                updateStringparameters7.Add("@part_no", detInfo[0].PartNo);
                updateStringparameters7.Add("@factory_id", detInfo[0].FactoryId);
                updateStringparameters7.Add("@factory_code", detInfo[0].FactoryCode);
                updateStringparameters7.Add("@enabled", "Y");
                updateStringparameters7.Add("@create_userid", loginId);
                updateStringparameters7.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                updateStringparameters7.Add("@guid", Guid.NewGuid());
                updateStringparameters7.Add("@rack_num", splitNum);
                updateStringparameters7.Add("@update_userid", null);
                updateStringparameters7.Add("@update_time", null);

                sqlStrings.Add(insterString2.ToString());
                parameterList.Add(updateStringparameters7);


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return oldStock;
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }


        /// <summary>
        /// 生产领料（一步）整箱领料
        /// </summary>
        /// <param name="detailInfo"></param>
        /// <param name="rackPackageInfo"></param>
        /// <param name="loginId"></param>
        /// <returns></returns>
        public string CheckFullBoxPicking(List<WmsMoveDetail_Extend> detailInfo, List<WmsRackPackage_Extend> rackPackageInfo, string loginId)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();

                List<string> sqlStrings = new List<string>();
                List<DynamicParameters> parameterList = new List<DynamicParameters>();

                for (int i = 0; i < rackPackageInfo.Count; i++)
                {
                    for (int j = 0; j < detailInfo.Count; j++)
                    {
                        if (detailInfo[j].PartNo == rackPackageInfo[i].PartNo)
                        {
                            //更新条码表，数量、库位
                            StringBuilder updateString = new StringBuilder(1024);
                            updateString.Append("update sys_stock set status=@status,locate_id=@locate_id,locate_name=@locate_name  where carton_no=@carton_no ");
                            DynamicParameters updateStringparameters = new DynamicParameters();
                            updateStringparameters.Add("@status", 60);
                            updateStringparameters.Add("@locate_id", detailInfo[j].destLocateId);
                            updateStringparameters.Add("@locate_name", detailInfo[j].destLocateName);
                            updateStringparameters.Add("@carton_no", rackPackageInfo[i].CortonNo);
                            updateStringparameters.Add("@update_userid", loginId);
                            updateStringparameters.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            sqlStrings.Add(updateString.ToString());
                            parameterList.Add(updateStringparameters);



                            //插入 wms_move_sn 条码表
                            StringBuilder updateString8 = new StringBuilder(1024);
                            updateString8.Append(@"INSERT INTO [dbo].[wms_move_sn]
                                        ([order_no]
                                        ,[item_no]
                                        ,[part_id]
                                        ,[part_no]
                                        ,[part_spec]
                                        ,[carton_no]
                                        ,[qty]
                                        ,[unit]
                                        ,[status]
                                        ,[factory_id]
                                        ,[factory_code]
                                        ,[enabled]
                                        ,[create_userid]
                                        ,[create_time]
                                        ,[update_userid]
                                        ,[update_time]
                                        ,[guid])
                                         VALUES (@order_no,@item_no,@part_id,@part_no,@part_spec,@carton_no,@qty,@unit,@status,@factory_id,@factory_code,@enabled,@create_userid,@create_time,@update_userid,@update_time,@guid)");
                            DynamicParameters updateStringparameters8 = new DynamicParameters();
                            updateStringparameters8.Add("@order_no", detailInfo[j].OrderNo);
                            updateStringparameters8.Add("@item_no", detailInfo[j].ItemNo);
                            updateStringparameters8.Add("@part_id", detailInfo[j].PartId);
                            updateStringparameters8.Add("@part_no", detailInfo[j].PartNo);
                            updateStringparameters8.Add("@part_spec", detailInfo[j].PartSpec);
                            updateStringparameters8.Add("@carton_no", rackPackageInfo[i].CortonNo);
                            updateStringparameters8.Add("@qty", rackPackageInfo[i].RackNum);
                            updateStringparameters8.Add("@unit", detailInfo[j].Unit);
                            updateStringparameters8.Add("@status", (int)WmsEnumUtil.MoveOrderSnStatus.BATCHED);
                            updateStringparameters8.Add("@factory_id", detailInfo[j].FactoryId);
                            updateStringparameters8.Add("@factory_code", detailInfo[j].FactoryCode);
                            updateStringparameters8.Add("@enabled", "Y");
                            updateStringparameters8.Add("@update_userid", loginId);
                            updateStringparameters8.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            updateStringparameters8.Add("@create_userid", loginId);
                            updateStringparameters8.Add("@create_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                            updateStringparameters8.Add("@guid", Guid.NewGuid());

                            sqlStrings.Add(updateString8.ToString());
                            parameterList.Add(updateStringparameters8);


                            //修改移库主表  30 处理中
                            StringBuilder updateString3 = new StringBuilder(1024);
                            updateString3.Append(" update wms_move_header set order_status=@order_status,update_userid=@update_userid,update_time=@update_time where order_no=@order_no ");
                            DynamicParameters updateStringparameters3 = new DynamicParameters();
                            updateStringparameters3.Add("@order_status", (int)WmsEnumUtil.MoveOrderStatus.PROCESSING);
                            updateStringparameters3.Add("@order_no", detailInfo[j].OrderNo);
                            updateStringparameters3.Add("@update_userid", loginId);
                            updateStringparameters3.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            sqlStrings.Add(updateString3.ToString());
                            parameterList.Add(updateStringparameters3);

                            //修改移库明细表  30 处理中，更新配料数量
                            StringBuilder updateString2 = new StringBuilder(1024);
                            updateString2.Append(@"update wms_move_detail set pick_qty = @pick_qty, out_qty=@out_qty ,rec_qty=@rec_qty ,item_status=@item_status,update_userid=@update_userid,update_time=@update_time where ruid=@ruid ");
                            DynamicParameters updateStringparameters2 = new DynamicParameters();
                            updateStringparameters2.Add("@item_status", (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING);
                            updateStringparameters2.Add("@ruid", detailInfo[j].Ruid);
                            updateStringparameters2.Add("@pick_qty", detailInfo[j].Qty);
                            updateStringparameters2.Add("@rec_qty", detailInfo[j].Qty);
                            updateStringparameters2.Add("@out_qty", detailInfo[j].Qty);
                            updateStringparameters2.Add("@update_userid", loginId);
                            updateStringparameters2.Add("@update_time", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

                            sqlStrings.Add(updateString2.ToString());
                            parameterList.Add(updateStringparameters2);

                        }
                    }
                }


                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();

                bool successCount = true;
                for (int i = 0; i < sqlStrings.Count; i++)
                {
                    if (dbConn.Execute(sqlStrings[i], parameterList[i], transaction) < 1)
                    {
                        successCount = false;
                    }
                }
                if (successCount)
                {
                    transaction.Commit();
                    return "OK";
                }
                else
                {
                    transaction.Rollback();
                    return null;
                }
            }
        }


        public WmsMoveHeader_Extend GetMoveHeaderInfo(string orderno)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  SELECT * FROM dbo.wms_move_header WHERE order_no=@order_no AND enabled='Y' ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@order_no", orderno);

                return dbConn.QueryFirstOrDefault<WmsMoveHeader_Extend>(sql.ToString(), parameters);
            }
        }


        public SysLocate GetLocateZTbin()
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append("  SELECT * FROM dbo.sys_locate WHERE locate_desc LIKE '%在途%' AND enabled='Y'  ");
                DynamicParameters parameters = new DynamicParameters();
                //parameters.Add("@carton_no", cartonNo);

                return dbConn.QueryFirstOrDefault<SysLocate>(sql.ToString(), parameters);
            }
        }

    }
}
